<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/3/14
 * Time: 13:08
 */

namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;
use app\index\validate\Valid;

class Bfmbgl extends Common
{
    /**
     * 月拜访目标管理
     */
    public function monthbfaimmanagment(){
        $bfr_name = $this->request->param('bfr_name');
        $gw_name = $this->request->param('gw_name');
        $ssbm = $this->request->param('ssbm');
        $this->assign('bfr_name',$bfr_name);
        $this->assign('gw_name',$gw_name);
        $this->assign('ssbm',$ssbm);
        $where = [];
        if($bfr_name){
            $where['bfr_name'] = ['like','%'.$bfr_name.'%'];
        }
        if($gw_name){
            $where['gw_name'] = ['like','%'.$gw_name.'%'];
        }
        if($ssbm){
            $where['ssbm'] = $ssbm;
        }
        $cnt = db('yd_ybfmb')->where($where)->count();
        $data = db('yd_ybfmb')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //部门
        $bm = db('department')->field('id,de_name')->select();
        $this->assign('bm',$bm);

        return $this->fetch();
    }

    public function addmbfaim(){
        if($_POST){
            $xm = $this->request->post('xm/a');
            $wdybfs = $this->request->post('wdybfs');
            $jxsybfs = $this->request->post('jxsybfs');
            $insert = [];
            foreach($xm as $k=>$v){
                $insert[] = [
                    'bfr'=>$v,
                    'bfr_name'=>db('employee')->where('id',$v)->value('em_name'),
                    'gw'=>db('job_employee')->where('employee_id',$v)->value('job_id'),
                    'gw_name'=>db('job t')->join('job_employee t2','t.id=t2.job_id')->where('t2.employee_id',$v)->value('t.job_name'),
                    'ssbm'=>db('department_employee')->where('employee_id',$v)->value('department_id'),
                    'ssbm_name'=>db('department t')->join('department_employee t2','t.id=t2.department_id')->where('t2.employee_id',$v)->value('t.de_name'),
                    'wdybfmbs'=>$wdybfs,
                    'jxsybfmbs'=>$jxsybfs,
                ];
            }
            $bool = db('yd_ybfmb')->insertAll($insert);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        return $this->fetch();
    }

    //修改
    public function updatembfaim(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['wdybfmbs'] = $this->request->post('wdybfs');
            $data['jxsybfmbs'] = $this->request->post('jxsybfs');
            $bool = db('yd_ybfmb')->where('id',$id)->update($data);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('yd_ybfmb')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    //删除
    public function drop(){
        $id = $this->request->param('id');
        $bool = db('yd_ybfmb')->where('id',$id)->delete();
        if(false!==$bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    public function lendbfaim(){
        return $this->fetch();
    }
}